package com.example.movie.data.movie.model

import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import cn.forward.androids.utils.ThreadUtil
import com.example.movie.R
import com.example.movie.common.constants.LoadingState
import com.example.movie.common.model.BaseResponse
import com.example.movie.common.model.RequestResult
import com.example.movie.common.model.Result
import com.example.movie.data.movie.model.bean.ApproveBean
import com.example.movie.data.movie.model.vo.ForumVo

class CommentViewModel(private val repository: MovieRepository) : ViewModel() {

    private val _commentList = MutableLiveData<RequestResult<BaseResponse<List<ForumVo>>>>()
    val commentList: MutableLiveData<RequestResult<BaseResponse<List<ForumVo>>>>
        get() = _commentList

    private val _replyList = MutableLiveData<RequestResult<BaseResponse<List<ForumVo>>>>()
    val replyList: MutableLiveData<RequestResult<BaseResponse<List<ForumVo>>>>
        get() = _replyList

    private val _addCommentState = MutableLiveData<Int>()
    val addCommentState: MutableLiveData<Int>
        get() = _addCommentState

    private val _approveState = MutableLiveData<ApproveBean>()
    val approveState: MutableLiveData<ApproveBean>
        get() = _approveState

    private val _addReplyState = MutableLiveData<Int>()
    val addReplyState: MutableLiveData<Int>
        get() = _addReplyState


    fun loadForumPage(movieId: Int, page: Int?, size: Int?) {
        var result: Result<BaseResponse<List<ForumVo>>>? = null;
        val handler = object : Handler(Looper.myLooper()!!) {
            override fun handleMessage(msg: Message) {
                if (msg.what == LoadingState.OK) {
                    if (result is Result.Success) {
                        _commentList.value =
                            RequestResult(success = (result as Result.Success<BaseResponse<List<ForumVo>>>).data)
                    } else {
                        _commentList.value = RequestResult(error = R.string.request_fail)
                    }
                }
            }
        }

        ThreadUtil.getInstance().execute {
            result = repository.loadForumPage(movieId, page, size)
            val message = Message()
            message.what = LoadingState.OK
            handler.sendMessage(message)
        }
    }

    fun addForum(movieId: Int, forum: String, pid: Int?, toId: String?) {
        var result: Result<Int>? = null;
        val handler = object : Handler(Looper.myLooper()!!) {
            override fun handleMessage(msg: Message) {
                if (msg.what == LoadingState.OK) {
                    if (pid == 0) {
                        _addCommentState.value = (result as Result.Success<Int>).data
                    } else {
                        _addReplyState.value = (result as Result.Success<Int>).data
                    }
                }
            }
        }

        ThreadUtil.getInstance().execute {
            result = repository.addForum(movieId, forum, pid, toId)
            val message = Message()
            message.what = LoadingState.OK
            handler.sendMessage(message)
        }
    }

    fun approve(forumId: Int) {
        var result: Result<ApproveBean>? = null;
        val handler = object : Handler(Looper.myLooper()!!) {
            override fun handleMessage(msg: Message) {
                if (msg.what == LoadingState.OK) {
                    _approveState.value = (result as Result.Success<ApproveBean>).data
                }
            }
        }

        ThreadUtil.getInstance().execute {
            result = repository.approve(forumId)
            val message = Message()
            message.what = LoadingState.OK
            handler.sendMessage(message)
        }
    }

    /**
     * 加载评论的回复数据
     */
    fun loadReplies(pid: Long, page: Int, size: Int) {
        var result: Result<BaseResponse<List<ForumVo>>>? = null;
        val handler = object : Handler(Looper.getMainLooper()) {
            override fun handleMessage(msg: Message) {
                if (msg.what == LoadingState.OK) {
                    if (result is Result.Success) {
                        _replyList.value =
                            RequestResult(success = (result as Result.Success<BaseResponse<List<ForumVo>>>).data)
                    } else {
                        _replyList.value = RequestResult(error = R.string.request_fail)
                    }
                }
            }
        }

        ThreadUtil.getInstance().execute {
            result = repository.loadReplies(pid, page, size)
            val message = Message()
            message.what = LoadingState.OK
            handler.sendMessage(message)
        }
    }


}